home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 3 / Light ROM 3 - Disc 2.iso / programs / pc / l_parser / readraw.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  7KB  |  194 lines

  1. /*
  2.     READRAW.C
  3.     Copyright (C) 1994  Earl C. Terwilliger
  4.             158 Eades Drive
  5.             Irvine, KY 40336
  6.             Phone (606)-723-5718
  7.             Internet: aisterwi@acs.eku.edu
  8. */
  9. #define DXF_VERTICES 4
  10. #include <stdio.h>
  11. #include <fcntl.h>
  12. #include <io.h>
  13. #include <sys\types.h>
  14. #include <sys\stat.h>
  15. int fp1;
  16. static unsigned long polygons = 0l, totpoints = 0l, nonpols = 0l;
  17. static unsigned long triangles = 0l, twoptpols = 0l;
  18. static unsigned int xyccpoly=0,yzccpoly=0,xzccpoly=0;
  19. static unsigned int xycpoly=0,yzcpoly=0,xzcpoly=0;
  20. static unsigned int xypols=0,yzpols=0,xzpols=0;
  21. static double area;
  22.  
  23. #pragma pack(1)
  24.  
  25. static struct  {
  26.     unsigned int layer;
  27.     unsigned int vertices;
  28.     unsigned int color;
  29.     unsigned long index[4];
  30.     unsigned int unique;
  31.     float xyz[4][3];
  32. } POLY;
  33.  
  34. main(argc,argv)
  35.     int argc;
  36.     char *argv[];
  37. {
  38.     unsigned int c,d,e;
  39.     char infile[64];
  40.     if (argc < 2) syntax(argv[0]);
  41.     strcpy(infile,argv[1]); 
  42.     if (!strchr(infile,'.')) strcat(infile,".RAW");
  43.     if (!(fp1 = open(infile,O_BINARY|O_RDONLY))) {
  44.     printf("Cannot open input file %s!\n",infile);
  45.     exit(1);
  46.     }
  47.     while(read(fp1,&POLY,sizeof(POLY))) {
  48.     polygons  += 1;
  49.     totpoints += POLY.vertices;
  50.     if (POLY.vertices == 3) ++triangles;
  51.     if (POLY.vertices == 2) ++twoptpols;
  52.     printf("Layer=%d Color=%d Vertices=%d Index=%4ld %4ld %4ld %4ld  Unique=",
  53.         POLY.layer,POLY.color,POLY.vertices,
  54.         POLY.index[0],POLY.index[1],POLY.index[2],POLY.index[3]);
  55.     for(c=0;c<DXF_VERTICES;++c) {
  56.         if (!(POLY.unique & (0x01<<c))) continue;
  57.         printf("%d ",c+1);
  58.     }
  59.     c = poly_area_xy();
  60.     if (area < 0) printf("\nPolygon is clockwise on the ");
  61.     if (area > 0) printf("\nPolygon is counterclockwise on the ");
  62.     switch (c) {
  63.         case 0:
  64.         printf("\nPolygon not visible (2-point polygon?)\n");
  65.         break;
  66.         case 1:
  67.         printf("XY plane\n");
  68.         break;
  69.         case 2:
  70.         printf("YZ plane\n");
  71.         break;
  72.         case 3:
  73.         printf("XZ plane\n");
  74.         break;
  75.         default:
  76.         printf("\n");
  77.         break;
  78.     }
  79.     for(c=0;c<4;++c) 
  80.       printf("x=%12.6f y=%12.6f z=%12.6f\n",POLY.xyz[c][0],POLY.xyz[c][1],POLY.xyz[c][2]);
  81.     }
  82.     close(fp1);
  83.     printf("\nPoints    %ld\nPolygons  %ld  ",totpoints,polygons);
  84.     printf("[%ld triangles]  [%ld 2-point polygons]\n",triangles,twoptpols);
  85.     printf("Front [xy] Polygons  %2u  [%2u clockwise]  [%2u counterclockwise]\n",
  86.     xypols,xycpoly,xyccpoly);
  87.     printf("Side  [yz] Polygons  %2u  [%2u clockwise]  [%2u counterclockwise]\n",
  88.     yzpols,yzcpoly,yzccpoly);
  89.     printf("Top   [xz] Polygons  %2u  [%2u clockwise]  [%2u counterclockwise]\n",
  90.     xzpols,xzcpoly,xzccpoly);
  91.     printf("Non planar Polygons  %2u  [Includes 2-point polygons]\n",nonpols);
  92.     printf("\nProgram complete.\n");
  93.     exit(0);
  94. }
  95. syntax(ptr)
  96.     char *ptr;
  97. {
  98.     printf("Syntax: %s infile[.RAW]\n",ptr);
  99.     exit(99);
  100. }
  101. /*
  102.  
  103.     area = 0.5 * ( ( x[0] * y[1] ) + ( x[1] * y[2] ) + ( x[2] * y[0] ) -
  104.            ( x[1] * y[0] ) - ( x[2] * y[1] ) - ( x[0] * y[2] ) );
  105.  
  106. and the area of a planar polygon is given by
  107.  
  108.     area = 0.0;
  109.     for ( i = 0; i < n - 1; i++ ) 
  110.     area += ( x[i] * y[i + 1] ) - ( x[i + 1] * y[i] );
  111.     area += ( x[n - 1] * y[0] ) - ( x[0] * y[n - 1] );
  112.     area /= 2.0;
  113.  
  114. If the area is a negative number, the polygon or triangle is clockwise, 
  115. if positive, it is counterclockwise. This is for the x-y plane of view. 
  116. If the polygon area is 0 then the polygon is not "viewed" from the x-y plane.
  117.  
  118. */
  119. poly_area_xy()
  120. {
  121.     int c=0,d=0;
  122.     if (POLY.vertices == 3) {
  123.       area = 0.5 * ( ( (double)POLY.xyz[0][0] * (double)POLY.xyz[1][2] ) + 
  124.              ( (double)POLY.xyz[1][0] * (double)POLY.xyz[2][2] ) + 
  125.              ( (double)POLY.xyz[2][0] * (double)POLY.xyz[0][2] ) - 
  126.              ( (double)POLY.xyz[1][0] * (double)POLY.xyz[0][2] ) - 
  127.              ( (double)POLY.xyz[2][0] * (double)POLY.xyz[1][2] ) - 
  128.              ( (double)POLY.xyz[0][0] * (double)POLY.xyz[2][2] ) );
  129.     } 
  130.     else {
  131.       area = 0.0;
  132.       for (c=0;c<POLY.vertices-1;++c)
  133.       area += ( (double)POLY.xyz[c][0]   * (double)POLY.xyz[c+1][2] ) - 
  134.           ( (double)POLY.xyz[c+1][0] * (double)POLY.xyz[c][2] );
  135.       area += ( (double)POLY.xyz[POLY.vertices-1][0] * (double)POLY.xyz[0][2] ) - 
  136.           ( (double)POLY.xyz[0][0] * (double)POLY.xyz[POLY.vertices-1][2] );
  137.       area /= 2.0;
  138.     }
  139.     if (area < 0.0)  { ++xycpoly;  ++xypols;  return(1); }
  140.     if (area > 0.0)  { ++xyccpoly; ++xypols; return(1); }
  141.     if (area == 0.0)  return(poly_area_yz());
  142.     return(0);
  143. }
  144. poly_area_yz()
  145. {
  146.     int c=0,d=0;
  147.     if (POLY.vertices == 3) {
  148.       area = 0.5 * ( ( (double)POLY.xyz[0][1] * (double)POLY.xyz[1][2] ) + 
  149.              ( (double)POLY.xyz[1][1] * (double)POLY.xyz[2][2] ) + 
  150.              ( (double)POLY.xyz[2][1] * (double)POLY.xyz[0][2] ) - 
  151.              ( (double)POLY.xyz[1][1] * (double)POLY.xyz[0][2] ) - 
  152.              ( (double)POLY.xyz[2][1] * (double)POLY.xyz[1][2] ) - 
  153.              ( (double)POLY.xyz[0][1] * (double)POLY.xyz[2][2] ) );
  154.     } 
  155.     else {
  156.       area = 0.0;
  157.       for (c=0;c<POLY.vertices-1;++c)
  158.       area += ( (double)POLY.xyz[c][1]   * (double)POLY.xyz[c+1][2] ) - 
  159.           ( (double)POLY.xyz[c+1][1] * (double)POLY.xyz[c][2] );
  160.       area += ( (double)POLY.xyz[POLY.vertices-1][1] * (double)POLY.xyz[0][2] ) - 
  161.           ( (double)POLY.xyz[0][1] * (double)POLY.xyz[POLY.vertices-1][2] );
  162.       area /= 2.0;
  163.     }
  164.     if (area < 0.0) { ++yzcpoly; ++yzpols; return(2); }
  165.     if (area > 0.0) { ++yzccpoly; ++yzpols; return(2); }
  166.     if (area == 0.0)  return(poly_area_xz());
  167.     return(0);
  168. }
  169. poly_area_xz()
  170. {
  171.     int c=0,d=0;
  172.     if (POLY.vertices == 3) {
  173.       area = 0.5 * ( ( (double)POLY.xyz[0][0] * (double)POLY.xyz[1][1] ) + 
  174.              ( (double)POLY.xyz[1][0] * (double)POLY.xyz[2][1] ) + 
  175.              ( (double)POLY.xyz[2][0] * (double)POLY.xyz[0][1] ) - 
  176.              ( (double)POLY.xyz[1][0] * (double)POLY.xyz[0][1] ) - 
  177.              ( (double)POLY.xyz[2][0] * (double)POLY.xyz[1][1] ) - 
  178.              ( (double)POLY.xyz[0][0] * (double)POLY.xyz[2][1] ) );
  179.     } 
  180.     else {
  181.       area = 0.0;
  182.       for (c=0;c<POLY.vertices-1;++c)
  183.       area += ( (double)POLY.xyz[c][0]   * (double)POLY.xyz[c+1][1] ) - 
  184.           ( (double)POLY.xyz[c+1][0] * (double)POLY.xyz[c][1] );
  185.       area += ( (double)POLY.xyz[POLY.vertices-1][0] * (double)POLY.xyz[0][1] ) - 
  186.           ( (double)POLY.xyz[0][0] * (double)POLY.xyz[POLY.vertices-1][1] );
  187.       area /= 2.0;
  188.     }
  189.     if (area < 0.0) { ++xzcpoly; ++xzpols; return(3); }
  190.     if (area > 0.0) { ++xzccpoly; ++xzpols; return(3); }
  191.     if (area == 0.0) ++nonpols;       
  192.     return(0);
  193. }
  194.